<?php

require_once 'cms/core/om/om/BaseCategoryI18n.php';


/**
 * Skeleton subclass for representing a row from the 'category_i18n' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package cms.core.om
 */	
class CategoryI18n extends BaseCategoryI18n {

    private $count;
    private $count2;
    private $latest;
    private $usedLanguages;

    public function getCount() {
        return $this->count;
    }

    public function setCount($count) {
        $this->count = $count;
    }

    public function getCount2() {
        return $this->count2;
    }

    public function setCount2($count2) {
        $this->count2 = $count2;
    }

    public function hydrate(ResultSet $rs, $startcol = 1)
    {
        $startcol = parent::hydrate($rs, $startcol);

        try {
            $this->count = $rs->getInt($startcol);
            $this->count2 = $rs->getInt(++$startcol);
        } catch(Exception $e) {
            ;
        }
    }


    public function getLatestReply() {

        if($this->latest === null) {

            require_once("cms/forum/om/TopicReplyPeer.php");

            $c = new Criteria();
            $c->add(TopicPeer::CATEGORY_ID, $this->getCategoryId());
            $c->add(TopicPeer::ACTIVE, true);
            $c->add(TopicReplyPeer::ACTIVE, true);

            $c->addJoin(TopicReplyPeer::TOPIC_ID, TopicPeer::TOPIC_ID);
            $c->addDescendingOrderByColumn(TopicReplyPeer::INSERT_DATE);
            $c->setLimit(1);

            $this->latest = TopicReplyPeer::doSelectOne($c);

        }

        return $this->latest;

    }


    /**
     * Does child pageI18n have a sibling with the
     * specified language code ?
     *
     * @param code The language code
     */
    function hasLanguage($code) {
        $c = new Criteria();
        $c->add(CategoryI18nPeer::CATEGORY_ID, $this->getCategoryId());
        $c->add(CategoryI18nPeer::LANGUAGE_CODE, $code);
        return CategoryI18nPeer::doCount($c) > 0;
    }


    /**
     * Get the already used languages for this pageI18n.
     *
     */
    public function getUsedLanguages() {
        if(is_null($this->usedLanguages)) {
            $criteria = new Criteria();
            $criteria->clearSelectColumns()->clearOrderByColumns();
            $criteria->addSelectColumn(CategoryI18nPeer::LANGUAGE_CODE);
            $criteria->add(CategoryI18nPeer::CATEGORY_ID, $this->getCategoryId());
            $rs = CategoryI18nPeer::doSelectRS($criteria);
            $codes = array();
            while($rs->next())
                $codes[] = $rs->getString(1);
            $this->usedLanguages = $codes;
        }
        return $this->usedLanguages;
    }


    /**
     * Get the available languages for this pageI18n.
     *
     */
    public function getAvailableLanguages() {
        if(is_null($this->usedLanguages)) {
            $used = $this->getUsedLanguages();
            $criteria = new Criteria();
            $criteria->add(LanguagePeer::CODE, $used, Criteria::NOT_IN);
            $criteria->add(LanguagePeer::I18N, true);
            $criteria->addAscendingOrderByColumn(LanguagePeer::NAME);
            $this->usedLanguages = LanguagePeer::doSelect($criteria);
        }
        return $this->usedLanguages;
    }


    /**
     * Get the name of this pageI18n in the CMS default language.
     *
     * @param default The default CMS language code.
     */
    function getDefaultName($default) {
        if($this->getLanguageCode() == $default)
            return $this->getName();
        else {
            $c = new Criteria();
            $c->clearSelectColumns();
            $c->addSelectColumn(CategoryI18nPeer::NAME);
            $c->add(CategoryI18nPeer::CATEGORY_ID, $this->getCategoryId());
            $c->add(CategoryI18nPeer::LANGUAGE_CODE, $default);
            $c->setLimit(1);
            $rs = CategoryI18nPeer::doSelectRs($c);
            if ($rs->next())
                return $rs->getString(1);
        }
    }


    /**
     * Get the not default language pageI18ns with the same page_id
     *
     * @param default The default CMS language code.
     */
    public function getForeignCategory18ns($default) {

        $c = new Criteria();
        $c->add(CategoryI18nPeer::CATEGORY_ID, $this->getCategoryId());
        $c->add(CategoryI18nPeer::LANGUAGE_CODE, $default, Criteria::NOT_EQUAL);
        return CategoryI18nPeer::doSelect($c);

    }


    /*
     * OVERRIDE THIS FROM THE BASE CLASS. OTHERWISE POPULATING FORM WILL NOT WORK IN ACTIONCONTROLLER
     * WHEN FOREIGN KEY OBJECT ARE USED
     */
    public function setCategoryId($v)
    {
        if ($this->category_id !== $v) {
            $this->category_id = $v;
            $this->modifiedColumns[] = CategoryI18nPeer::CATEGORY_ID;
        }
    }

} // CategoryI18n
